<?php
class UserServiceController extends Zend_Controller_Action
{
	/**
	 * 
	 * Index action will be redirected to the home page
	 * 
	 *  
	 */
	function indexAction()
	{
		$this->_redirect('userservice/listuserservice');
	}
	
	/**
	 * List service action
	 * 
	 * 
	 */
	function listuserserviceAction()
	{
		$this->view->title = "Manage user services";
		$userService = new UserService();
		$this->view->userservices = $userService->fetchAll();				
	}
	
	/**
	 * Add service action
	 * 
	 */
	function adduserserviceAction()
	{
		$this->view->title = "Add new service";
		
		$form = new ServiceForm();		
		$this->view->form = $form;
				
		if($this->_request->isPost()){
			$formData = $this->_request->getPost();
			if($form->isValid($formData)){
				$service = new Service();
				$row = $service->createRow();
				$row->code = $form->getValue('codeServiceForm');
				$row->description = $form->getValue('descServiceForm');
				$row->save();
				$this->_redirect('service/listservice');
			} else {
				$form->populate($formData);
			}
		}
	}
	
	/**
	 * Delete service action
	 * 
	 */
	function deleteuserServiceAction(){
		$this->view->title = "Delete service";
		$registry = Zend_Registry::getInstance();
		$translator = $registry->get('Zend_Translate');		
		if($this->_request->isPost()){
			$id = (int)$this->_request->getPost('id');
			$del = $this->_request->getPost('del');
			if($del == $translator->translate("Yes") && $id > 0){
				$services = new Service();
				$where = 'id= ' . $id;
				$services->delete($where);
			}
			$this->_redirect('service/listservice');
		} else {
			$id = (int)$this->_request->getParam('id');
			if($id > 0){
				$services = new Service();
				$this->view->service = $services->fetchRow('id=' . $id);
			}
		}
	}
	
	/**
	 * Edit service action
	 */
	function edituserserviceAction(){
		$this->view->title = "Edit service";
		$registry = Zend_Registry::getInstance();
		$translator = $registry->get('Zend_Translate');
		
		// init service form
		$form = new ServiceForm();
		$this->view->form = $form;
		// check to see whether a post request(update) 
		if($this->_request->isPost()){
			$formData = $this->_request->getPost();
			if($form->isValid($formData)){
				$id = (int)$form->getValue('id');
				$services = new Service();
				$row = $services->fetchRow("id=" . $id);
				$row->code = $form->getValue('codeServiceForm');
				$row->description = $form->getValue('descServiceForm');
				$row->save();
				$this->_redirect('service/listservice');				
			} else {
				$form->populate($formData);
			}
		} else { // not a post request(show)
			$id = (int)$this->_request->getParam('id');			
			if($id > 0){
				$services = new Service();
				// fetch the required service
				$service = $services->fetchRow('id=' . $id);
				
				// populate the form
				$form->getElement('id')->setValue($id);
				$form->getElement('codeServiceForm')->setValue($service->code);
				$form->getElement('descServiceForm')->setValue($service->description);
				$form->getElement('submit')->setLabel($translator->translate('Edit'));
								
			} else {
				$this->_redirect('service/listservice');
			}
		}
	}
}
?>